package com.gpframework.module.controller.wages;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.wages.domain.GzWarehousePersonnel;
import com.gpframework.module.wages.domain.req.GzWarehousePersonnelAdd;
import com.gpframework.module.wages.domain.req.GzWarehousePersonnelReq;
import com.gpframework.module.wages.domain.req.GzWarehousePersonnelUpdate;
import com.gpframework.module.wages.domain.vo.GzWarehousePersonnelVo;
import com.gpframework.module.wages.service.IGzWarehousePersonnelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GzWarehousePersonnelController
 * @Author gpframework
 * @Date 2024-01-22
 * @Version 1.0.0
 * @Description 仓库工资条Controller
 */
@Validated
@Api(tags = "仓库人员工资")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/wages/warehousepersonnel" )
public class GzWarehousePersonnelController extends BaseController {

    private final IGzWarehousePersonnelService iGzWarehousePersonnelService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2024-01-22
  * @Param gzWarehousePersonnel
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('wages:warehousepersonnel:list')")
    @GetMapping("/list")
    public TableDataInfo<GzWarehousePersonnelVo> list(GzWarehousePersonnelReq gzWarehousePersonnelReq)
    {
        startPage();
        QueryWrapper<GzWarehousePersonnel> lqw = new QueryWrapper<GzWarehousePersonnel>();
        gzWarehousePersonnelReq.generatorQuery(lqw,true);
        List<GzWarehousePersonnelVo> list = iGzWarehousePersonnelService.listVo(lqw);
        return getDataTable(list);
    }


    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('wages:warehousepersonnel:list')")
    @GetMapping("/listStatistics")
    public TableDataInfo<GzWarehousePersonnelVo> listStatistics(GzWarehousePersonnelReq gzWarehousePersonnelReq)
    {
        startPage();
        QueryWrapper<GzWarehousePersonnel> lqw = new QueryWrapper<GzWarehousePersonnel>();
        gzWarehousePersonnelReq.generatorQuery(lqw,true);
        List<GzWarehousePersonnelVo> list = iGzWarehousePersonnelService.listStatistics(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-22
     * @Param gzWarehousePersonnel
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('wages:warehousepersonnel:add')" )
    @Log(title = "添加仓库工资条" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GzWarehousePersonnelAdd gzWarehousePersonnelAdd) {
        GzWarehousePersonnel gzWarehousePersonnel = Convert.convert(new TypeReference<GzWarehousePersonnel>() {}, gzWarehousePersonnelAdd);
        return toAjax(iGzWarehousePersonnelService.save(gzWarehousePersonnel) ? 1 : 0);
    }

    @ApiOperation("是否上报审批")
    @PreAuthorize("@ss.hasPermi('wages:warehousepersonnel:edit')" )
    @Log(title = "是否上报审批" , businessType = BusinessType.UPDATE)
    @PutMapping("/peported")
    public AjaxResult peported(@Valid @RequestBody GzWarehousePersonnelUpdate gzWarehousePersonnelUpdate) {
        for (GzWarehousePersonnel gzWarehousePersonnel : gzWarehousePersonnelUpdate.getGzWarehousePersonnelList()) {
            gzWarehousePersonnel.setEscalation(Long.valueOf("1"));
            iGzWarehousePersonnelService.updateById(gzWarehousePersonnel);
        }

        return AjaxResult.success();
    }

    @ApiOperation("审批修改")
    @PreAuthorize("@ss.hasPermi('wages:warehousepersonnel:edit')" )
    @Log(title = "审批修改仓库工资条" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GzWarehousePersonnelUpdate gzWarehousePersonnelUpdates) {
        for (GzWarehousePersonnel gzWarehousePersonnel : gzWarehousePersonnelUpdates.getGzWarehousePersonnelList()) {
            if ((Long.valueOf("1").equals(gzWarehousePersonnel.getEscalation()))){
                GzWarehousePersonnel gzWarehousePersonnelUpdate = Convert.convert(new TypeReference<GzWarehousePersonnel>() {}, gzWarehousePersonnel);
                iGzWarehousePersonnelService.updateById(gzWarehousePersonnelUpdate);
            }else {
                return AjaxResult.error("请选择已经审核的人员");
            }
        }
        return AjaxResult.success();
    }



    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('wages:warehousepersonnel:updateByIds')" )
    @Log(title = "批量修改仓库工资条" , businessType = BusinessType.UPDATE)
    @RequestMapping
    public AjaxResult updateByIds(@Valid @RequestBody List<GzWarehousePersonnelUpdate> gzWarehousePersonnelUpdates) {
        List<GzWarehousePersonnel> gzWarehousePersonnels = Convert.toList(GzWarehousePersonnel.class, gzWarehousePersonnelUpdates);
        return toAjax(iGzWarehousePersonnelService.saveOrUpdateBatch(gzWarehousePersonnels) ? 1 : 0);
    }
    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2024-01-22
     * @Param gzWarehousePersonnel
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('wages:warehousepersonnel:export')" )
    @Log(title = "导出仓库工资条" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GzWarehousePersonnelReq gzWarehousePersonnelReq) {
        QueryWrapper<GzWarehousePersonnel> lqw = new QueryWrapper<GzWarehousePersonnel>();
        gzWarehousePersonnelReq.generatorQuery(lqw,true);
        List<GzWarehousePersonnelVo> list = iGzWarehousePersonnelService.listVo(lqw);
        ExcelUtil<GzWarehousePersonnelVo> util = new ExcelUtil<GzWarehousePersonnelVo>(GzWarehousePersonnelVo. class);
        return util.exportExcel(list, "warehousepersonnel" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-01-22
     * @Param gzWarehousePersonnel
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('wages:warehousepersonnel:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GzWarehousePersonnelVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGzWarehousePersonnelService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2024-01-22
     * @Param gzWarehousePersonnel
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('wages:warehousepersonnel:remove')" )
    @Log(title = "删除仓库工资条" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGzWarehousePersonnelService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
